import Cookies from 'js-cookie'
var timeInterval = ''
import {getSidebarData} from '../../api/config/config'
const app = {
  state: {
    sidebar: getSidebarData(),
    leftMenu: {
      isShow: false
    },
    head: {
      title: "首页",
      path:"/",
      left: {
        icon: "",
        leftClick: ""
      },
      right: {
        icon: "",
        rightClick: ""
      }
    },
    theme: 'default',
    getPupil: '',
    isLoading: false,
    sendCodeTime:0,
    livenewsChannels: Cookies.get('livenewsChannels') || '[]',
    visitedViews: [],
    selectUserData: {}, // 记录选中的用户信息
    selectTorHistoryUid: '',
    isShare: false, // 分享是否开启
    secret_key: '',
    secret_userid: '',
    ajaxLoading: false, // 是否显示加载中
    shareUrl: '' // 分享链接
  },
  mutations: {
    UPDATE_SIDERBAR: (state, name) => {
      function changeData() {
        var sidebarData = state.sidebar // 注意 这里的数据直接被双向绑定了  而且还不能直接用 state.sidebar = 对象 只能拆分处理它
        for (var i in sidebarData) {
          var attr = sidebarData[i]
          var list = attr.list
          sidebarData[i].isShow = false
          for (var z in list) {
            var name2 = list[z].id
            sidebarData[i].list[z].active = false
            if (name2 === name) {
              sidebarData[i].isShow = true
              sidebarData[i].list[z].active = true
            }
          }
        }
      }
      changeData()
      // state.sidebar = getSidebarData()
    },
    SHOW_SHARE: (state) => {
      let isShader = state.isShare
      if (isShader) {
        return
      } else {
        state.isShare = true
        setTimeout(function () {
          state.isShare = false
        }, 3000)
      }
    },
    UPDATE_CODETIME:(state,data)=>{
        if(timeInterval){

        }else{
          state.sendCodeTime=60;
          timeInterval=setInterval(function () {
              state.sendCodeTime--;
              console.log(state.sendCodeTime);
              if( state.sendCodeTime<0){
                state.sendCodeTime=0
                clearInterval(timeInterval);
                timeInterval=null;
              }
          },1000);
        }
    },
    STOP_CODETIME: (state) => { // 停止倒计时
      if (timeInterval) {
        clearInterval(timeInterval)
        timeInterval = null
        state.sendCodeTime=0
      }
    },
    UPDATE_LOADING:(state,data)=>{
      state.isLoading=data
    },
    UPDATA_MENUSHOW:(state,data)=>{
      state.leftMenu.isShow = data;
    },
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1);
      } else {
        Cookies.set('sidebarStatus', 0);
      }
      state.sidebar.opened = !state.sidebar.opened;
    },
    ADD_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.some(v => v.path === view.path)) return
      state.visitedViews.push({name: view.name, path: view.path})
    },
    DEL_VISITED_VIEWS: (state, view) => {
      let index
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          index = i
          break
        }
      }
      state.visitedViews.splice(index, 1)
    }
  },
  actions: {
    ToggleSideBar: ({commit}) => {
      commit('TOGGLE_SIDEBAR')
    },
    addVisitedViews: ({commit}, view) => {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews: ({commit}, view) => {
      commit('DEL_VISITED_VIEWS', view)
    }
  }
};

export default app;
